<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客列表页</title>
    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="css/blog_list.css">
</head>
<body>
    <!--导航栏-->
    <div class="nav">
        <img src="image/头像.jpg" alt="">
        <span class="title">ybo的博客系统</span>
        <!--spance用来占位-->
        <span class="spance"></span>
        <!--按钮-->
        <a href="blog_list.html">主页</a>
        <a href="blog_edit.html">写博客</a>
        <a href="javascript:logout()">注销</a>
    </div>

    <!--页面主体-->
    <div class="container">
        <div class="container-left">
            <!--用这个.card来表示用户信息-->
            <div class="card">
                <img src="image/草帽.jpg" alt="">
                <h3>ybo</h3>
                <a href="#">github 地址</a>
                <div class="counter">
                    <span>文章</span>
                    <span>分类</span>
                </div>
                <div class="counter">
                    <span>2</span>
                    <span>3</span>
                </div>
            </div>
        </div>
        <div class="container-right"></div>
    </div>

    <script src="js/app.js"></script>
    <script src="https://code.jquery.com/jquery-3.6.1.min.js"></script>
    <script>
        // 通过这个函数来从服务器获得博客列表的数据集
        function getBlogs(){
            $.ajax({
                type: 'get',
                url: 'blog',
                success: function(body){
                    //根据返回来的 json 数据，来构造页面内容 div.blog
                    //jquery ajax 会自动的把响应得到的 body 按照响应的 Content - Type 进行转换
                    //如果响应的 Content-type 是 json ，此时就会自动把 body 转成 js 的对象
                    console.log(body.data)
                    let container = document.querySelector('.container-right');
                    for(let blog of body.data){
                        console.log('hahahh')
                        let blogDiv = document.createElement('div');
                        blogDiv.className = 'blog';
                        // 创建博客标题
                        let titleDiv = document.createElement('div');
                        titleDiv.className='title';
                        titleDiv.innerHTML = blog.title;
                        blogDiv.appendChild(titleDiv);
                        // 创建日期
                        let dataDiv = document.createElement('div');
                        dataDiv.className = 'date';
                        dataDiv.innerHTML = blog.postTime;
                        blogDiv.appendChild(dataDiv);
                        // 创建摘要
                        let descDiv = document.createElement('div');
                        descDiv.className = 'desc';
                        descDiv.innerHTML = blog.content;
                        blogDiv.appendChild(descDiv);
                        // 创建查看全文按钮
                        let a = document.createElement('a');
                        a.innerHTML = '查看全文 >>';
                        a.href = 'blog_detail.html?blogId=' + blog.blogId;
                        blogDiv.appendChild(a);
                        container.appendChild(blogDiv);
                    }
                }

            });

        }
        //获取博客列表
        getBlogs();
        

        //获取当前用户信息
        function getUserInfo() {
            $.ajax({
               type:'get',
               url: 'userInfo',
               success: function(body){
                   // 让后端在查询失败的时候，不要返回200，而是返回403
                   //避免在前端触发 success 分支
                   let h3 = document.querySelector('.card h3');
                   h3.innerHTML = body.data.username;
                } 
            });
        }

        getUserInfo();

        // 注销
        function logout() {
        $.ajax({
            type: 'get',
            url: 'logout',
            success: function() {
                location.href = "blog_list.html"
            }
        });
}
    </script>
</body>
</html>